using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using QLNhaSach;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

using System.Collections.Specialized;

namespace WindowsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                //check database exists
                Server SqlServer;
                String DatabaseName = "QLNhaSach";
                //string cwd = System.Reflection.Assembly.GetExecutingAssembly().Location;
                //string projectName = "QLNhaSach";
                //string solutionPath = cwd.Replace(projectName + ".exe", "") + "connectionString.txt";
                //string text = System.IO.File.ReadAllText(solutionPath);
                //if (text.Contains("SQLEXPRESS"))
                //{
                //    SqlServer = new Server(@".\SQLEXPRESS");
                //}
                //else
                //{
                //    SqlServer = new Server(@".\");
                //}
                SqlServer = new Server(@".\");
                
                ServerConnection SqlServerConnection = SqlServer.ConnectionContext;

                SqlServerConnection.LoginSecure = true;
                SqlServerConnection.DatabaseName = "master";

                if (SqlServer.Databases[DatabaseName] == null)
                {
                    Database NewDatabase = new Database(SqlServer, DatabaseName);

                    FileGroup DatabaseFileGroup = new FileGroup(NewDatabase, "PRIMARY");
                    NewDatabase.FileGroups.Add(DatabaseFileGroup);

                    DataFile DatabaseDataFile = new DataFile(DatabaseFileGroup, DatabaseName);
                    DatabaseFileGroup.Files.Add(DatabaseDataFile);

                    DatabaseDataFile.FileName = Application.StartupPath + "\\" + DatabaseName + ".mdf";

                    LogFile DatabaseLogFile = new LogFile(NewDatabase, DatabaseName + "_log");
                    NewDatabase.LogFiles.Add(DatabaseLogFile);

                    DatabaseLogFile.FileName = Application.StartupPath + "\\" + DatabaseName + "_log.ldf";

                    StringCollection DatabaseFilesCollection = new StringCollection();

                    DatabaseFilesCollection.Add(DatabaseDataFile.FileName);
                    DatabaseFilesCollection.Add(DatabaseLogFile.FileName);

                    SqlServer.AttachDatabase(DatabaseName, DatabaseFilesCollection);
                }
            }
            catch
            {
                MessageBox.Show("Co loi khi thiet lap co so du lieu", "Hospital Manager");
            }

            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.UserSkins.BonusSkins.Register();
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");

            Application.Run(new LoginUser());
        }
    }
}